<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <!-- <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item> -->
    <!-- <el-form-item label="调度" label-width="150px" prop="dispatchId"> -->
      <!-- <el-input v-model="dataForm.dispatchId" placeholder="调度id">

      </el-input> -->
      <!-- <el-select style="width: 100%" v-model="dataForm.dispatchId" placeholder="请选择">
          <el-option
            v-for="item in dataList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
      </el-select>
      </el-form-item>
    </el-form-item> -->
    <el-form-item label="收运单位" label-width="150px" prop="ctUnitId">
      <el-select  style="width: 100%" v-model="dataForm.ctUnitId"placeholder="请选择">
          <el-option
            v-for="item in unitDataList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
      </el-select>
      <!-- <el-input v-model="dataForm.ctUnitId" placeholder="收运单位id"></el-input> -->
    </el-form-item>
    <el-form-item label="调度车辆" label-width="150px" prop="dispatchCarId">
      <!-- <el-input v-model="dataForm.dispatchCarId" placeholder="调度车辆id"></el-input> -->
      <el-select style="width: 100%" v-model="dataForm.dispatchCarId"placeholder="请选择">
          <el-option
            v-for="item in carDataList"
            :key="item.id"
            :label="item.numberPlate"
            :value="item.id">
          </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="污水池" label-width="150px" prop="productionContainerId">
      <el-select style="width: 100%" v-model="dataForm.productionContainerId"placeholder="请选择">
          <el-option
            v-for="item in containerDataList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
      </el-select>
      <!-- <el-input v-model="dataForm.productionContainerId" placeholder="污水池id"></el-input> -->
    </el-form-item>
    <!-- <el-form-item label="预警" label-width="150px" prop="alarmId">
      <el-select style="width: 100%" v-model="dataForm.dispatchCarId"placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.dispatchCarId"
            :label="item.remark"
            :value="item.dispatchCarId">
          </el-option>
      </el-select> -->
      <!-- <el-input v-model="dataForm.alarmId" label-width="150px" placeholder="预警id"></el-input> -->
    <!-- </el-form-item> -->
    <el-form-item label="计划状态" label-width="150px" prop="status">
      <!-- (0:未执行, 1:接单并执行, 4:完成, 5:拒绝) -->
      <el-select style="width: 100%" v-model="dataForm.status" placeholder="请选择">
          <el-option
            v-for="item in [
              {value: 0, label: '未执行'},
              {value: 1, label: '接单并执行'},
              {value: 4, label: '完成'},
              {value: 5, label: '拒绝'},
            ]"
            :key="item.value"
            :label="item.label"
            :value="`${item.value}`">
          </el-option>
      </el-select>
      <!-- <el-input v-model="dataForm.status" placeholder="计划状态(0:未执行, 1:接单并执行, 4:完成, 5:拒绝)"></el-input> -->
    </el-form-item>
    <!-- <el-form-item label="此次任务收运重量(完成时填写)" prop="syWeight">
      <el-input v-model="dataForm.syWeight" placeholder="此次任务收运重量(完成时填写)"></el-input>
    </el-form-item> -->
    <el-form-item label="任务开始时间" label-width="150px" prop="startTime">
      <el-input v-model="dataForm.startTime" placeholder="任务开始时间"></el-input>
    </el-form-item>
    <el-form-item label="任务结束结束时间" label-width="150px" prop="endTime">
      <el-input v-model="dataForm.endTime" placeholder="任务结束结束时间"></el-input>
    </el-form-item>
    <el-form-item label="备注" label-width="150px" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <!-- <el-form-item label="收运距离" prop="distance">
      <el-input v-model="dataForm.distance" placeholder="收运距离"></el-input>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        carDataList: [],
        unitDataList: [],
        dataList: [],
        containerDataList: [],
        dataForm: {
          id: 0,
          dispatchId: '',
          ctUnitId: '',
          dispatchCarId: '',
          executeCarNumber: '',
          productionUnitId: '',
          productionContainerId: '',
          alarmId: '',
          executeDay: '',
          status: '',
          syWeight: '',
          startTime: '',
          endTime: '',
          distance: '',
          remark: ''
        },
        dataRule: {
          // dispatchId: [
          //   { required: true, message: '调度id不能为空', trigger: 'blur' }
          // ],
          ctUnitId: [
            { required: true, message: '收运单位id不能为空', trigger: 'blur' }
          ],
          dispatchCarId: [
            { required: true, message: '调度车辆id不能为空', trigger: 'blur' }
          ],
          executeCarNumber: [
            { required: true, message: '执行车辆车牌号不能为空', trigger: 'blur' }
          ],
          productionUnitId: [
            { required: true, message: '产生单位id不能为空', trigger: 'blur' }
          ],
          productionContainerId: [
            { required: true, message: '生产单位污水污水池id不能为空', trigger: 'blur' }
          ],
          // alarmId: [
          //   { required: true, message: '预警id不能为空', trigger: 'blur' }
          // ],
          executeDay: [
            { required: true, message: '收运日期 2021-01-01不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '计划状态不能为空', trigger: 'blur' }
          ],
          syWeight: [
            { required: true, message: '此次任务收运重量(完成时填写)不能为空', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '任务开始时间不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '任务结束结束时间不能为空', trigger: 'blur' }
          ],
          distance: [
            { required: true, message: '收运距离不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: false, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    created () {
      this.getCarDataList()
      this.getUnitDataList()
      this.getContainerDataList()
      this.getDataList()
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sign/signtask/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.dispatchId = data.signTask.dispatchId
                this.dataForm.ctUnitId = data.signTask.ctUnitId
                this.dataForm.dispatchCarId = data.signTask.dispatchCarId
                this.dataForm.executeCarNumber = data.signTask.executeCarNumber
                this.dataForm.productionUnitId = data.signTask.productionUnitId
                this.dataForm.productionContainerId = data.signTask.productionContainerId
                this.dataForm.alarmId = data.signTask.alarmId
                this.dataForm.executeDay = data.signTask.executeDay
                this.dataForm.status = data.signTask.status
                this.dataForm.syWeight = data.signTask.syWeight
                this.dataForm.startTime = data.signTask.startTime
                this.dataForm.endTime = data.signTask.endTime
                this.dataForm.distance = data.signTask.distance
                this.dataForm.remark = data.signTask.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sign/signtask/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'dispatchId': this.dataForm.dispatchId,
                'ctUnitId': this.dataForm.ctUnitId,
                'dispatchCarId': this.dataForm.dispatchCarId,
                'executeCarNumber': this.dataForm.executeCarNumber,
                'productionUnitId': this.dataForm.productionUnitId,
                'productionContainerId': this.dataForm.productionContainerId,
                'alarmId': this.dataForm.alarmId,
                'executeDay': this.dataForm.executeDay,
                'status': this.dataForm.status,
                'syWeight': this.dataForm.syWeight,
                'startTime': this.dataForm.startTime,
                'endTime': this.dataForm.endTime,
                'distance': this.dataForm.distance,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 获取调度数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/sign/signcardispatch/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 获取收运单位
      getUnitDataList () {
        this.$http({
          url: this.$http.adornUrl('/sign/signunit/list'),
          method: 'get',
          params: this.$http.adornParams({
            'limit': -1
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.unitDataList = data.page.list
          } else {
            this.unitDataList = []
          }
        })
      },
      // 获取调度车辆
      getCarDataList () {
        this.$http({
          url: this.$http.adornUrl('/sign/signcar/list'),
          method: 'get',
          params: this.$http.adornParams({
            'limit': -1
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.carDataList = data.page.list
          } else {
            this.carDataList = []
          }
        })
      },
      // 获取污水池
      getContainerDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/sign/signcontainer/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.containerDataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.containerDataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      }
    }
  }
</script>
